package com.xu.drools.web;

import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * @author:whh
 * @date: 2025-08-17 14:06
 * <p></p>
 */
@Controller
public class CommonController {




    @GetMapping("/tool")
    public String tools(){
        return "Tool";
    }





    @PostMapping("/upload")
    @ResponseBody
    public Map<String, Object> uploadSingleFile(
            @RequestParam("file") MultipartFile file) {

        Map<String, Object> result = new HashMap<>();

        // 检查文件是否为空
        if (file.isEmpty()) {
            result.put("success", false);
            result.put("message", "请选择要上传的文件");
            return result;
        }

        try {

            // 获取原始文件名并处理中文
            String originalFilename = file.getOriginalFilename();
            // 构建文件保存路径
           File save=new File("/tmp",originalFilename);
            file.transferTo(save);
            // 准备返回结果
            result.put("success", true);
            result.put("message", "文件上传成功");
            result.put("originalFileName", originalFilename);
            result.put("savedFileName", originalFilename);
            result.put("fileSize", file.getSize() + " bytes");
            result.put("contentType", file.getContentType());

            return result;

        } catch (IOException e) {
            result.put("success", false);
            result.put("message", "文件上传失败: " + e.getMessage());
            return result;
        }
    }
}
